class WebconfigsController < ApplicationController
  # GET /webconfigs
  # GET /webconfigs.xml
  layout "admin"
  before_filter :admin?,:except=>[:show]
  cache_sweeper :link_sweeper,:only=>[:update,:create,:destroy]
  def index
    @webconfigs = Webconfig.find(:all,:conditions=>["style=?",params[:style]])
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @webconfigs }
    end
  end

  # GET /webconfigs/1
  # GET /webconfigs/1.xml
  def show
    @webconfig = Webconfig.find_by_name(params[:name])
    @page_title=@webconfig.description
    @page_nav="><a href='/aboutus/detail'>帮助中心</a>>&nbsp;#{@webconfig.description}"
    respond_to do |format|
      format.html {render :layout=>"front_page" }
      format.xml  { render :xml => @webconfig }
    end
  end

  # GET /webconfigs/new
  # GET /webconfigs/new.xml
  def new
    @webconfig = Webconfig.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @webconfig }
    end
  end

  # GET /webconfigs/1/edit
  def edit
    if params[:id]=="0"
      @webconfig=Webconfig.find_by_style(params[:style]) || Webconfig.create(:name=>"name",:description=>"des",:style=>params[:style])
    else
       @webconfig = Webconfig.find(params[:id])
    end
   
  end

  # POST /webconfigs
  # POST /webconfigs.xml
  def create
    @webconfig = Webconfig.new(request.parameters[:webconfig])

    respond_to do |format|
      if @webconfig.save
        flash[:notice] = 'Webconfig was successfully created.'
        format.html { redirect_to(webconfigs_path("style"=>@webconfig.style)) }
        format.xml  { render :xml => @webconfig, :status => :created, :location => @webconfig }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @webconfig.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /webconfigs/1
  # PUT /webconfigs/1.xml
  def update
    @webconfig = Webconfig.find(params[:id])

    respond_to do |format|
      if @webconfig.update_attributes(params[:webconfig])
        flash[:notice] = 'Webconfig was successfully updated.'
        format.html { redirect_to(webconfigs_path("style"=>@webconfig.style)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @webconfig.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /webconfigs/1
  # DELETE /webconfigs/1.xml
  def destroy
    @webconfig = Webconfig.find(params[:id])
    @webconfig.destroy

    respond_to do |format|
      format.html { redirect_to(webconfigs_url) }
      format.xml  { head :ok }
    end
  end
end
